package leetcode.t2String;

/**
 * 12.整数转罗马数字
 * 1. 定义符号代表的值，大于就放一个同时减去代表的值，知道最后为0跳出循环
 * 2. 递归处理，效率低
 *
 * @author K
 * @date 2024/1/23 14:34
 */
public class T12_IntToRoman {
    static int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    static String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public static String intToRoman(int num) {
        StringBuffer roman = new StringBuffer();
        for (int i = 0; i < values.length; ++i) {
            int value = values[i];
            String symbol = symbols[i];
            while (num >= value) {
                num -= value;
                roman.append(symbol);
            }
            if (num == 0) {
                break;
            }
        }
        return roman.toString();
    }

    public static String intToRoman1(int num) {
        return find(num);
    }

    private static String find(int num) {
        if (num > 3999) {
            return "";
        }

        if (num <= 5) {
            if (num == 4) return "IV";
            else if (num == 5) return "V";
            else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < num; i++) {
                    sb.append("I");
                }
                return sb.toString();
            }
        } else if (num <= 10) {
            if (num == 9) return "IX";
            else if (num == 10) return "X";
            else return "V" + find(num % 5);
        } else if (num <= 50) {
            if (num == 50) return "L";
            else if (num >= 40) return "XL" + find(num % 40);
            else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < num / 10; i++) {
                    sb.append("X");
                }
                return sb.toString() + find(num % 10);
            }
        } else if (num <= 100) {
            if (num == 100) return "C";
            else if (num >= 90) return "XC" + find(num % 90);
            else return "L" + find(num % 50);
        } else if (num <= 500) {
            if (num == 500) return "D";
            else if (num >= 400) return "CD" + find(num % 400);
            else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < num / 100; i++) {
                    sb.append("C");
                }
                return sb.toString() + find(num % 100);
            }
        } else if (num <= 1000) {
            if (num == 1000) return "M";
            else if (num >= 900) return "CM" + find(num % 900);
            else return "D" + find(num % 500);
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < num / 1000; i++) {
                sb.append("M");
            }
            return sb.toString() + find(num % 1000);
        }
    }

    public static void main(String[] args) {
//        String result = intToRoman(3);
//        String result1 = intToRoman(4);
//        String result2 = intToRoman(9);
        String result3 = intToRoman(58);
        String result4 = intToRoman(1994);
        System.out.println();
    }

}
